package com.nbcio.pm.controller;

import java.util.List;
import java.util.Arrays;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.nbcio.common.core.validate.AddGroup;
import com.nbcio.common.core.validate.EditGroup;
import com.nbcio.common.web.core.BaseController;
import com.nbcio.common.core.domain.R;
import com.nbcio.common.excel.utils.ExcelUtil;
import com.nbcio.common.mybatis.core.page.TableDataInfo;
import com.nbcio.pm.domain.vo.PmTeamMemberVo;
import com.nbcio.pm.domain.bo.PmTeamMemberBo;
import com.nbcio.pm.service.IPmTeamMemberService;
import lombok.RequiredArgsConstructor;

/**
 * 团队成员
 *
 * @author nbmade
 * @date 2025-01-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pm/team/member")
public class PmTeamMemberController extends BaseController {

    private final IPmTeamMemberService pmTeamMemberService;

    /**
     * 查询团队成员列表
     */
    @SaCheckPermission("pm:team:manage")
    @GetMapping("/list")
    public TableDataInfo<PmTeamMemberVo> list(PmTeamMemberBo bo) {
        return pmTeamMemberService.queryPageList(bo);
    }

    /**
     * 导出团队成员列表
     */
    @SaCheckPermission("pm:team:manage")
    @PostMapping("/export")
    public void export(PmTeamMemberBo bo, HttpServletResponse response) {
        List<PmTeamMemberVo> list = pmTeamMemberService.queryList(bo);
        ExcelUtil.exportExcel(list, "团队成员", PmTeamMemberVo.class, response);
    }

    /**
     * 获取团队成员详细信息
     */
    @SaCheckPermission("pm:team:manage")
    @GetMapping("/{id}")
    public R<PmTeamMemberVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(pmTeamMemberService.queryById(id));
    }

    /**
     * 新增团队成员
     */
    @SaCheckPermission("pm:team:manage")
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PmTeamMemberBo bo) {
        return toAjax(pmTeamMemberService.insertByBo(bo));
    }

    /**
     * 修改团队成员
     */
    @SaCheckPermission("pm:team:manage")
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PmTeamMemberBo bo) {
        return toAjax(pmTeamMemberService.updateByBo(bo));
    }

    /**
     * 删除团队成员
     */
    @SaCheckPermission("pm:team:manage")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(pmTeamMemberService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 批量添加团队成员
     */
    @SaCheckPermission("pm:team:manage")
    @PostMapping("/batch")
    public R<Void> batchAdd(@Validated(AddGroup.class) @RequestBody PmTeamMemberBo bo) {
        return toAjax(pmTeamMemberService.batchInsertByBo(bo));
    }
}